﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Signage01.dao
{
    public class CampaignDAO
    {
        string serializationFile = "resources/screen/campaign.bin";
        //private List<Timeline> listTimeline;
        static CampaignDAO campaignDAO = null;


        private CampaignDAO()
        {

        }

        public static CampaignDAO getSingleTon()
        {
            if (campaignDAO == null)
            {
                campaignDAO = new CampaignDAO();
            }
            return campaignDAO;
        }

        // initially the list has only one campaign, but we want to keep the provision to add more campaign,
        // so we are using list
        public List<Campaign> getAll()
        {
            // retrieving the list from the file
            try
            {
                using (Stream stream = File.Open(serializationFile, FileMode.OpenOrCreate))
                {
                    var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                    // if this line throws exception, the file is empty, so we have to create a new list in catch block
                    List<Campaign> listTemp = (List<Campaign>)bformatter.Deserialize(stream);
                    return listTemp;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("The file is empty");
                List<Campaign> listTemp = new List<Campaign>();
                return listTemp;
            }
        }

        
        public List<Timeline> getTimelineListByCampaign(Campaign campaign)
        {
            List<Timeline> listTimeline;
            List<Campaign> listCampaign = getAll();

            int index = 0;
            int indexCampaignFound = -1;
            foreach (Campaign tempCampaign in listCampaign)
            {
                if (tempCampaign.Name == campaign.Name)
                {
                    indexCampaignFound = index;
                }
                index++;
            }

            listTimeline = listCampaign[indexCampaignFound].getTimeline();
            return listTimeline;
        }
        

        public int deleteCampaign(Campaign campaign)
        {
            List<Campaign> listCampaign = getAll();

            int index = 0;
            int deleteIndex = -1;

            foreach (Campaign tempCampaign in listCampaign)
            {
                if (tempCampaign.Name == campaign.Name)
                {
                    deleteIndex = index;
                    break;
                }
                index++;
            }

            listCampaign.RemoveAt(deleteIndex);
            saveListToFile(listCampaign);
            return 1;
        }


        // again, this list has only one campaign
        public int saveListToFile(List<Campaign> listCampaign)
        {
            try
            {
                // saving the list to the file
                using (Stream stream = File.Open(serializationFile, FileMode.OpenOrCreate))
                {
                    var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    bformatter.Serialize(stream, listCampaign);
                    stream.Close();
                }
                return 1;
            }
            catch (Exception ex)
            {
                Console.WriteLine("save excepiton is:" + ex.ToString());
                return 0;
            }
        }

        

        public void updateCampaign(Campaign campaign) // this campaign holds the added timeline
        {
            List<Campaign> listCampaign = getAll();

            int index = 0;
            int indexCampaignFound = -1;
            foreach (Campaign tempCampaign in listCampaign)
            {
                if (tempCampaign.Name == campaign.Name)
                {
                    indexCampaignFound = index;
                    break;
                }
                index++;
            }

            listCampaign[indexCampaignFound] = campaign;

            saveListToFile(listCampaign);
        }


        public void deleteTimelineFromCampaign(Campaign campaign,Timeline timeline) // this campaign holds the added timeline
        {
            List<Timeline> listTimeline = campaign.getTimeline();


            int index = 0;
            int indexTimelineFound = -1;
            foreach (Timeline tempTimeline in listTimeline)
            {
                if (tempTimeline.Name == timeline.Name)
                {
                    indexTimelineFound = index;
                    break;
                }
                index++;
            }

            listTimeline.RemoveAt(indexTimelineFound);

        }




    }
}
